package com.d3.pomodoro.component

import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatTextView
import androidx.appcompat.widget.SwitchCompat
import com.afollestad.materialdialogs.DialogCallback
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.internal.button.DialogActionButton
import com.afollestad.materialdialogs.list.SingleChoiceListener
import com.afollestad.materialdialogs.list.customListAdapter
import com.afollestad.materialdialogs.list.listItemsSingleChoice
import com.d3.pomodoro.R
import com.d3.pomodoro.adapter.ClockMusicAdapter
import com.orhanobut.logger.Logger
import me.shaohui.bottomdialog.BaseBottomDialog


class TomatoClockTipView : BaseBottomDialog(),
    View.OnClickListener {

    private var tomatoClockTipViewListener: TomatoClockTipViewListener? = null

    override fun getLayoutRes(): Int {
        return R.layout.view_tomato_clock_tips
    }

    override fun bindView(v: View?) {
        v?.findViewById<SwitchCompat>(R.id.switchCompatMusic).apply {
            this?.setOnCheckedChangeListener { buttonView, isChecked ->
                Logger.e("isChecked is $isChecked")
            }
        }

        v?.findViewById<AppCompatTextView>(R.id.textViewCustomer).apply {
            this?.setOnClickListener(this@TomatoClockTipView)
        }
        v?.findViewById<AppCompatTextView>(R.id.textViewClose).apply {
            this?.setOnClickListener(this@TomatoClockTipView)
        }
        v?.findViewById<ViewGroup>(R.id.musicRootLayout).apply {
            this?.setOnClickListener(this@TomatoClockTipView)
        }

    }

    override fun onClick(v: View?) {
        when {
            v?.id == R.id.textViewCustomer -> {
                Logger.e("textViewCustomer is click")

            }
            v?.id == R.id.textViewClose -> {
                Logger.e("textViewClose is click")

            }
            v?.id == R.id.musicRootLayout -> {
                Logger.e("musicRootLayout is click")
                val dataList = arrayListOf(
                    "aa",
                    "bb",
                    "cc", "aa",
                    "bb",
                    "cc", "aa",
                    "bb",
                    "cc", "aa",
                    "bb",
                    "cc"
                )
                var isAfterClickButton = false

                val dialog = MaterialDialog(context!!).show {
                    title(text = "专注中背景音效")
                    positiveButton(text = "确定", click = object : DialogCallback {
                        override fun invoke(p1: MaterialDialog) {
                            Logger.e("MaterialDialog is $p1")
                            isAfterClickButton = true
                        }
                    })
                    val button = findViewById<DialogActionButton>(R.id.md_button_positive)
                    button.updateTextColor(resources.getColor(R.color.d3_gray))
//                    customListAdapter(ClockMusicAdapter(dataList))
                    listItemsSingleChoice(
                        items = dataList,
                        initialSelection = 1,
                        waitForPositiveButton = false,
                        selection = object : SingleChoiceListener {
                            override fun invoke(
                                dialog: MaterialDialog,
                                index: Int,
                                text: CharSequence
                            ) {
                                if (isAfterClickButton) {
                                    isAfterClickButton = false
                                } else {
                                    Logger.e("index is $index")
                                }

                            }
                        })

                }

                dialog.show()
                tomatoClockTipViewListener?.action("musicRootLayout")
            }
        }
    }

    fun setTomatoClockTipViewListener(tomatoClockTipViewListener: TomatoClockTipViewListener?) {
        this.tomatoClockTipViewListener = tomatoClockTipViewListener
    }

    interface TomatoClockTipViewListener {
        fun action(action: String)
    }
}